<template>
  <div class="logs-container">
    <div class="header-section">
      <h2>日志管理</h2>
      <div class="stats-info">
        <el-card class="info-card">
          <div class="stat-item">
            <span class="label">总日志数:</span>
            <span class="value">{{ totalCount }}</span>
          </div>
        </el-card>
        <el-card class="info-card blockchain">
          <div class="blockchain-info">
            <h4>区块链信息</h4>
            <div class="blockchain-details">
              <div class="detail-item">
                <span class="label">区块高度:</span>
                <span class="value">{{ blockchainInfo.blockNumber }}</span>
              </div>
              <div class="detail-item">
                <span class="label">客户端版本:</span>
                <span class="value">{{ blockchainInfo.clientVersion }}</span>
              </div>
              <div class="detail-item">
                <span class="label">Gas价格:</span>
                <span class="value">{{ blockchainInfo.gasPrice }}</span>
              </div>
            </div>
          </div>
        </el-card>
      </div>
    </div>

    <div class="table-section">
      <el-card>
        <template #header>
          <div class="card-header">
            <span>操作日志列表</span>
            <el-button type="primary" @click="refreshData" :loading="loading">
              <el-icon><Refresh /></el-icon>
              刷新
            </el-button>
          </div>
        </template>

        <el-table 
          :data="logsData" 
          v-loading="loading"
          stripe
          border
          style="width: 100%"
          height="500"
        >
          <el-table-column type="index" label="序号" width="60" />
          <el-table-column prop="id" label="ID" width="80" />
          <el-table-column prop="module" label="模块" width="120" />
          <el-table-column prop="operation" label="操作" width="150" />
          <el-table-column prop="method" label="方法" width="120" />
          <el-table-column prop="params" label="参数" width="200" show-overflow-tooltip />
          <el-table-column prop="duration" label="耗时(ms)" width="100" sortable>
            <template #default="scope">
              <el-tag 
                :type="getDurationTagType(scope.row.duration)"
                size="small"
              >
                {{ scope.row.duration }}ms
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="timestamp" label="操作时间" width="180" sortable>
            <template #default="scope">
              {{ formatTime(scope.row.timestamp) }}
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" width="80">
            <template #default="scope">
              <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
                {{ scope.row.status === 1 ? '成功' : '失败' }}
              </el-tag>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Refresh } from '@element-plus/icons-vue'
import { getAllLogs, getLogCount, getEthereumInfo } from '@/api/logs.js'

// 响应式数据
const logsData = ref([])
const totalCount = ref(0)
const loading = ref(false)
const blockchainInfo = ref({
  blockNumber: 0,
  clientVersion: '',
  gasPrice: 0
})

// 获取所有日志数据
const fetchLogs = async () => {
  try {
    loading.value = true
    const response = await getAllLogs()
    if (response.data.code === 200) {
      logsData.value = response.data.data
    } else {
      ElMessage.error('获取日志数据失败')
    }
  } catch (error) {
    console.error('获取日志失败:', error)
    ElMessage.error('获取日志数据失败')
  } finally {
    loading.value = false
  }
}

// 获取日志总数
const fetchLogCount = async () => {
  try {
    const response = await getLogCount()
    if (response.data.code === 200) {
      totalCount.value = response.data.data
    } else {
      ElMessage.error('获取日志总数失败')
    }
  } catch (error) {
    console.error('获取日志总数失败:', error)
    ElMessage.error('获取日志总数失败')
  }
}

// 获取区块链信息
const fetchBlockchainInfo = async () => {
  try {
    const response = await getEthereumInfo()
    blockchainInfo.value = response.data
  } catch (error) {
    console.error('获取区块链信息失败:', error)
    ElMessage.error('获取区块链信息失败')
  }
}

// 刷新所有数据
const refreshData = async () => {
  await Promise.all([
    fetchLogs(),
    fetchLogCount(),
    fetchBlockchainInfo()
  ])
  ElMessage.success('数据刷新成功')
}

// 格式化时间
const formatTime = (timestamp) => {
  if (!timestamp) return ''
  const date = new Date(timestamp)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
}

// 根据耗时获取标签类型
const getDurationTagType = (duration) => {
  if (duration < 100) return 'success'
  if (duration < 500) return 'warning'
  return 'danger'
}

// 组件挂载时加载数据
onMounted(() => {
  refreshData()
})
</script>

<style scoped>
.logs-container {
  padding: 20px;
  background: #f5f5f5;
  min-height: 100vh;
}

.header-section {
  margin-bottom: 20px;
}

.header-section h2 {
  margin: 0 0 20px 0;
  color: #303133;
  font-size: 24px;
  font-weight: 600;
}

.stats-info {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
}

.info-card {
  flex: 1;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
}

.info-card.blockchain {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.info-card :deep(.el-card__body) {
  padding: 20px;
}

.stat-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.stat-item .label {
  font-size: 16px;
  opacity: 0.9;
}

.stat-item .value {
  font-size: 24px;
  font-weight: bold;
}

.blockchain-info h4 {
  margin: 0 0 15px 0;
  font-size: 18px;
  font-weight: 600;
}

.blockchain-details {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.detail-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
}

.detail-item .label {
  opacity: 0.9;
}

.detail-item .value {
  font-weight: 600;
}

.table-section {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header span {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

:deep(.el-table th) {
  background-color: #f8f9fa;
  color: #495057;
  font-weight: 600;
}

:deep(.el-table td) {
  padding: 12px 0;
}

:deep(.el-table .el-table__row:hover) {
  background-color: #f5f7fa;
}

:deep(.el-tag) {
  border-radius: 4px;
}
</style>
